#include "test13.h"
#include "ui_test13.h"

test13::test13(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::test13)
{
    ui->setupUi(this);
    initital();
}

test13::~test13()
{
    delete ui;
}

void test13::initital()
{
    my_server = new my_TCPServer;
    connect(my_server,SIGNAL(readresult(QString,int)),
                this,SLOT(readserver(QString,int)));

    connect(my_server,SIGNAL(connected(int)),
                this,SLOT(TCP_Conect(int)));
    connect(my_server,SIGNAL(disconnected(int)),
                this,SLOT(TCP_disConect(int)));
    my_client = new TcpClient;
    connect(my_client,SIGNAL(readbuf(QString)),
                this,SLOT(readclient(QString)));

    timer = new QTimer;
    connect(timer,&QTimer::timeout,this,&test13::on_pushButton_2_clicked);
    timer2 = new QTimer;
    connect(timer2,&QTimer::timeout,this,&test13::on_pushButton_5_clicked);
}

void test13::readserver(QString buf, int id)
{
    my_Socket *socket = my_server->Socketlist.value(id);
    ui->textEdit->append(socket->netattr.IP+":"+QString::number(socket->netattr.port)+"   "+buf);
}

void test13::readclient(QString buf)
{
    ui->textEdit_2->append(buf);
}

void test13::TCP_Conect(int id)
{
    my_Socket *socket = my_server->Socketlist.value(id);
    ui->textEdit->append(socket->netattr.IP+":"+QString::number(socket->netattr.port)+"连接成功");
}

void test13::TCP_disConect(int id)
{
    my_Socket *socket = my_server->Socketlist.value(id);
    ui->textEdit->append(socket->netattr.IP+":"+QString::number(socket->netattr.port)+"断开连接");
}

void test13::on_pushButton_clicked()
{
    if(ui->pushButton->text()=="打开服务器")
    {
        if(my_server->open(ui->lineEdit->text(),ui->spinBox->value()))
            ui->pushButton->setText("停止帧听");
        else
            ui->textEdit->append("打开失败");
    }
    else
    {
        my_server->close();
        ui->pushButton->setText("打开服务器");
    }
}

void test13::on_pushButton_4_clicked()
{
    if(ui->pushButton_4->text()=="连接服务器")
    {
        if(my_client->connectHost(ui->lineEdit_3->text(),ui->spinBox_3->value()))
            ui->pushButton_4->setText("断开连接");
        else
            ui->textEdit_2->append("打开失败");
    }
    else
    {
        my_client->disconnect();
        ui->pushButton_4->setText("连接服务器");
    }
}

void test13::on_pushButton_2_clicked()
{
    QList<int>id_list= my_server->Socketlist.keys();
    QString buf = ui->lineEdit_2->text();
    for(int i=0;i<id_list.size();i++)
    {
        my_server->send(buf,id_list.at(i));
    }
}

void test13::on_pushButton_3_clicked()
{
    if(ui->pushButton_3->text()=="循环发送")
    {
        ui->pushButton_3->setText("停止发送");
        timer->start(ui->spinBox_2->value());
    }
    else
    {
        ui->pushButton_3->setText("循环发送");
        timer->stop();
    }

}

void test13::on_pushButton_5_clicked()
{
    my_client->send(ui->lineEdit_4->text().append(0x0a).toLatin1());
}

void test13::on_pushButton_6_clicked()
{
    if(ui->pushButton_6->text()=="循环发送")
    {
        ui->pushButton_6->setText("停止发送");
        timer2->start(ui->spinBox_2->value());
    }
    else
    {
        ui->pushButton_6->setText("循环发送");
        timer2->stop();
    }
}
